package com.service.Authorization.service.impl;

import com.service.Authorization.dao.PortalUserDao;
import com.service.Authorization.dto.ScreenUserDto;
import com.service.Authorization.entity.PortalUser;
import com.service.Authorization.entity.ProgrammeInfoExamine;
import com.service.Authorization.service.PortalUserService;
import com.service.Authorization.service.ProgrammeInfoExamineService;
import com.service.Authorization.task.ScheduledService;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * openApi用户表(PortalUser)表服务实现类
 *
 * @author makejava
 * @since 2021-08-10 16:14:27
 */
@Service("portalUserService")
public class PortalUserServiceImpl implements PortalUserService {
    @Resource
    private PortalUserDao portalUserDao;
    @Autowired
    private ScheduledService scheduledService;
    @Resource
    private ProgrammeInfoExamineService programmeInfoExamineService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PortalUser queryById(String id) {
        return this.portalUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<PortalUser> queryAllByLimit(int offset, int limit) {
        return this.portalUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param portalUser 实例对象
     * @return 实例对象
     */
    @Override
    public PortalUser insert(PortalUser portalUser) {
        this.portalUserDao.insert(portalUser);
        return portalUser;
    }

    /**
     * 修改数据
     *
     * @param portalUser 实例对象
     * @return 实例对象
     */
    @Override
    public PortalUser update(PortalUser portalUser) {
        this.portalUserDao.update(portalUser);
        return this.queryById(portalUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.portalUserDao.deleteById(id) > 0;
    }
    @Transactional
    @Override
    public List<PortalUser> insertOrUpdate(PortalUser strictMap, String userId) throws Exception {
        PortalUser screenUser = new PortalUser();
        screenUser.setTenantName(strictMap.getTenantName());
        screenUser.setManageId(userId);
        screenUser.setDomain(strictMap.getDomain());
        List<PortalUser> screenUsers = portalUserDao.queryAll(screenUser);
        if (screenUsers.size() > 0) {
            return screenUsers;
        } else {
            strictMap.setId(IdUtil.getStringId());
            strictMap.setManageId(userId);
            strictMap.setStatus(true);
            portalUserDao.insert(strictMap);
            ProgrammeInfoExamine programmeInfoExamine = new ProgrammeInfoExamine();
            programmeInfoExamine.setManageId(userId);
            List<ProgrammeInfoExamine> programmeInfoExamineList = programmeInfoExamineService.queryAll(programmeInfoExamine);
            if (programmeInfoExamineList.size() == 0) {
                programmeInfoExamineService.addDefaultProgrammeInfo(userId,null);
            }
            return null;
        }
    }

    @Override
    public void updateApiAccount(PortalUser strictMap) {
        portalUserDao.update(strictMap);
    }

    @Override
    public void serviceStatusBind(String screen_user_id, Boolean status, String userId, String domain) {
        PortalUser screenUser1 = new PortalUser();
        screenUser1.setManageId(userId);
        screenUser1.setDomain(domain);
        List<PortalUser> screenUsers = portalUserDao.queryAll(screenUser1);
        List<PortalUser> collect = screenUsers.stream().filter(screenUser2 -> !screenUser2.getId().equals(screen_user_id)).collect(Collectors.toList());
        collect.stream().forEach(screenUser2 -> {
            if (status) {
                screenUser2.setStatus(false);
            }
        });
        List<PortalUser> collect1 = screenUsers.stream().filter(screenUser2 -> screenUser2.getId().equals(screen_user_id)).collect(Collectors.toList());
        collect1.stream().forEach(screenUser -> {
            screenUser.setManageId(userId);
            screenUser.setId(screen_user_id);
            screenUser.setStatus(status);
        });
        portalUserDao.insertOrUpdateBatch(screenUsers);
    }

    @Override
    public List<ScreenUserDto> queryAll(String userId) {
        PortalUser screenUser = new PortalUser();
        screenUser.setManageId(userId);
        List<PortalUser> list = portalUserDao.queryAll(screenUser);
        return  ModelMapperUtil.strictMapList(list, ScreenUserDto.class);
    }

    @Override
    public List<PortalUser> queryAllS(PortalUser portalUser) {
        return portalUserDao.queryAll(portalUser);
    }
}
